//
//  chatDataSource.m
//  tcpoctest
//
//  Created by gump on 6/19/13.
//  Copyright (c) 2013 gump. All rights reserved.
//

#import "chatDataSource.h"
#import "ChatContentInfo.h"
#import "chatuserdb.h"
#import "ChatViewController.h"
#import "sendChatRead.h"

@implementation chatDataSource

-(id)init
{
    self = [super init];
    
    if (self != nil) {
        self.strSendChatContent = @"";
        chatDic = [[NSMutableDictionary alloc] init];
        self.currentChatArr = nil;
        chatSequenceDic = [[NSMutableDictionary alloc] init];
        chatIDDic = [[NSMutableDictionary alloc] init];
        
        ModifyStateArr = [[NSMutableArray alloc] init];
        iModifyState = CRS_READ;
    }
    
    return self;
}

-(void)SetSendChatContent:(NSString *)str
{
    pthread_mutex_lock(&mutexChat);
    
    self.strSendChatContent = str;
    
    pthread_mutex_unlock(&mutexChat);
}


-(NSString *)SendChatContent
{
    NSString *str = @"";
    
    pthread_mutex_lock(&mutexChat);
    
    str = self.strSendChatContent;
    
    pthread_mutex_unlock(&mutexChat);
    
    return str;
}

-(void)RemoveAllChatDic
{
    pthread_mutex_lock(&mutexChat);
    
    [chatDic removeAllObjects];
    [chatSequenceDic removeAllObjects];
    [chatIDDic removeAllObjects];
    
    pthread_mutex_unlock(&mutexChat);
}

-(void)SetChatDicArr:(NSMutableArray *)chatarr withKey:(NSString *)strkey
{
    pthread_mutex_lock(&mutexChat);
    
    [chatDic setObject:chatarr forKey:strkey];
    
    pthread_mutex_unlock(&mutexChat);
}

-(NSMutableArray *)GetArrByKey:(NSString *)strkey
{
    NSMutableArray *arr = nil;
    
    pthread_mutex_lock(&mutexChat);
    
    arr = [chatDic objectForKey:strkey];
    
    pthread_mutex_unlock(&mutexChat);
    
    return arr;
}

-(void)RemoveObjectByKey:(NSString *)strkey
{
    pthread_mutex_lock(&mutexChat);
    
    [chatDic removeObjectForKey:strkey];
    
    pthread_mutex_unlock(&mutexChat);
}

-(void)DelCurChatArrObjectByRow:(int)irow
{
    pthread_mutex_lock(&mutexChat);
    
    if (self.currentChatArr != nil) {
        ChatContentInfo *ccinfo = [self.currentChatArr objectAtIndex:irow];
        if (ccinfo != nil) {
            switch (ccinfo.chatType) {
                case CT_VOICE:
                case CT_IMAGE:
                {
                    [[FileManager shareControl] deleteFile:ccinfo.strtext];
                }
                    break;
                    
                default:
                    break;
            }
            
            
            [[DataBaseCtrol shareControl].chatuserdatabase deleChatInfo:ccinfo];
            
        }
        
        [self.currentChatArr removeObjectAtIndex:irow];
    }
    
    [[ViewManager sharedViewManager].chatvc refreshView];
    
    pthread_mutex_unlock(&mutexChat);
}

-(ChatContentInfo *)getLastChatInfoByMail:(NSString *)strmail
{
    ChatContentInfo *ccinfo = nil;
    
    pthread_mutex_lock(&mutexChat);
    
    NSMutableArray * arr = [chatDic objectForKey:strmail];
    if (arr != nil && arr.count > 0) {
        ccinfo = [arr lastObject];
    }
    
    pthread_mutex_unlock(&mutexChat);
    
    return ccinfo;
}

-(ChatContentInfo *)getChatInfoBySequence:(int)is
{
    ChatContentInfo *ccinfo = nil;
    
    pthread_mutex_lock(&mutexChat);
    
    ccinfo = [chatSequenceDic objectForKey:[NSNumber numberWithInt:is]];
    
    pthread_mutex_unlock(&mutexChat);
    
    return ccinfo;
}

-(ChatContentInfo *)getChatInfoByID:(NSString *)strid
{
    ChatContentInfo *ccinfo = nil;
    
    pthread_mutex_lock(&mutexChat);
    
    ccinfo = [chatIDDic objectForKey:strid];
    
    pthread_mutex_unlock(&mutexChat);
    
    return ccinfo;
}

-(void)addChatInfo:(ChatContentInfo *)ccinfo withSquence:(int)is
{
    pthread_mutex_lock(&mutexChat);
    
    [chatSequenceDic setObject:ccinfo forKey:[NSNumber numberWithInt:is]];
    
    pthread_mutex_unlock(&mutexChat);
}

-(void)addChatInfo:(ChatContentInfo *)ccinfo withID:(NSString *)strid
{
    pthread_mutex_lock(&mutexChat);
    
    [chatIDDic setObject:ccinfo forKey:strid];
    
    pthread_mutex_unlock(&mutexChat);
}

-(void)addModifyStateArr:(ChatContentInfo *)ccinfo
{
    pthread_mutex_lock(&mutexChat);
    
    if (![ModifyStateArr containsObject:ccinfo]) {
        [ModifyStateArr addObject:ccinfo];
    }
    
    pthread_mutex_unlock(&mutexChat);
}

-(void)ProcessModifyState
{
    pthread_mutex_lock(&mutexChat);
    
    for (ChatContentInfo *ccinfo in ModifyStateArr) {
        ccinfo.ichatReadState = iModifyState;
        
        [[DataBaseCtrol shareControl].chatuserdatabase UpdateChatState:ccinfo];
        
        if (iModifyState == CRS_READ) {
            [[netManager sharedNetManager].sendCR sendChatAlreadyRead:ccinfo.strid];
        }
    
    }
    
    [ModifyStateArr removeAllObjects];
    
    [[DataControl shareControl].UICommendDS addUICommand:UC_CHAT_REFRESH];
    
    pthread_mutex_unlock(&mutexChat);
}

-(void)SetModifyState:(int)istate
{
    pthread_mutex_lock(&mutexChat);
    
    iModifyState = istate;
    
    pthread_mutex_unlock(&mutexChat);
}

@end
